var gulp = require('gulp');
var concat = require('gulp-concat');
var order = require('gulp-order');
var uglify = require('gulp-uglify');
var htmlmin = require('gulp-htmlmin');
var ngHtml2js = require('gulp-ng-html2js');
var cleanCss = require('gulp-clean-css');

gulp.task('htmlmin', function() {
    gulp.src('partials/*.html')
        .pipe(htmlmin({ collapseWhitespace: true }))
        .pipe(ngHtml2js())
        .pipe(concat('partials.min.js'))
        .pipe(uglify())
        .pipe(gulp.dest('js/'));

    gulp.src('templates/*.html')
        .pipe(htmlmin({ collapseWhitespace: true }))
        .pipe(ngHtml2js())
        .pipe(concat('templates.min.js'))
        .pipe(uglify())
        .pipe(gulp.dest('js/'));
});

gulp.task('cssmin', function() {
    return gulp.src('css/*.css')
        .pipe(cleanCss({ compatibility: 'ie8' }))
        .pipe(concat('all.min.css'))
        .pipe(gulp.dest('dist'));
});

gulp.task('concat', function() {
    gulp.src(['js/*.js', 'js/controllers/*.js', '!js/all.js'])
        .pipe(order([
            'js/jquery.min.js',
            'js/js.cookie.js',
            'js/angular.js',
            'js/uikit.min.js',
            'js/angular-ui-router.js',
            'js/app.js',
            'js/service.js',
            'js/router.js',
            'js/controllers/*.js'
        ], { base: './' }))
        .pipe(concat('all.js'))
        .pipe(uglify({mangle:false}))
        .pipe(gulp.dest('dist/'));
});

/*gulp.task('uglify', function() {
    gulp.src('js/all.js')
        .pipe(uglify())
        .pipe(gulp.dest('dist/'));
});*/

gulp.task('default', ['htmlmin', 'cssmin', 'concat']);

gulp.watch(['js/*.js', 'js/controllers/*.js', '!js/all.js'], ['concat']);
gulp.watch(['css/*.css'], ['cssmin']);
gulp.watch(['partials/*.html','templates/*.html'],['htmlmin']);
